package com.pig4cloud.pigx.knowledge.support.rule;

import com.pig4cloud.pigx.knowledge.dto.AiMessageResultDTO;
import com.pig4cloud.pigx.knowledge.dto.ChatMessageDTO;
import com.pig4cloud.pigx.knowledge.service.AiFunctionAssistantService;
import com.pig4cloud.pigx.knowledge.support.provider.ModelProvider;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;

/**
 * 基于本地实时数据的聊天
 *
 * @author lengleng
 * @date 2024/3/26
 */
@Slf4j
@Component("functionChat")
@RequiredArgsConstructor
public class FunctionChatRule implements ChatRule {

	private final ModelProvider modelProvider;

	/**
	 * 处理聊天信息
	 * @param chatMessageDTO 聊天上文
	 * @return flux stream
	 */
	@Override
	public Flux<AiMessageResultDTO> process(ChatMessageDTO chatMessageDTO) {
		AiFunctionAssistantService aiFunctionAssistant = modelProvider
			.getAiFunctionAssistant(chatMessageDTO.getModelName())
			.getValue();
		String result = aiFunctionAssistant.chat(chatMessageDTO.getConversationId(), chatMessageDTO.getContent());
		return Flux.just(new AiMessageResultDTO(result));
	}

}
